<template>
    <div id="app">
        <el-container>
            <el-header>
                    <img src="../assets/1.png" alt="">问卷系统
                   <el-tag size="mini" type="info">{{userInfo.username}}</el-tag>
                    <el-button @click="logout()" type="text">注销</el-button>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu :router="true">
                       <el-menu-item :index="menu.router"
                       v-for="menu in menus" :key="'menu_'+menu.id"
                       >{{menu.menu_name}}</el-menu-item>
                     
                    </el-menu>
                </el-aside>
                <el-main>
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
export default {
    data(){
        return {
         menus:[

         ]
        }
    },
    computed:{
        userInfo(){
            return this.store.get('userInfo');
        }
    },
  created() {
    this.getUsers();
    this.getMenus();
    this.$root.$on('updateMenus',()=>{
        this.getMenus();
    });
  },
  beforeRouteEnter(to,from,next){
      next((vm)=>{
        vm.axios.get("/users/islogin").then((ret) => {
        const { data } = ret.data;
        if (data == 0) {
          vm.$router.push("/login");
        }
      });
      })
  },
  methods: {
      getMenus(){
          this.axios.get('/menus',{params:{
              status:1
          }}).then((ret)=>{
              const {data} = ret.data;
              this.menus = data;
          })
      },
    logout() {
      this.axios.get("/users/logout").then(() => {
        this.$router.push({
          name: "login"
        });
      });
    },
    getUsers() {
      this.axios
        .get("/users")
        .then(ret => {
          let { data, error } = ret.data;
        })
        .catch(data => {
       
        });
    }
  }
};
</script>

<style>
</style>
